package app.excel.cartoon.word;

import java.util.List;

import app.excel.base.ExcelEnum;
import app.excel.cartoon.style.ExcelCartoonStyle;
import cn.lily.com.container.data.Record;
import cn.lily.com.file.excel.io.ExcelWriter;
import cn.lily.com.lang.Basic;

/**
 * Excel-Word-Sheet处理类
 * @author JiaJ
 *
 */
public class ExcelWordSheet {
	
	private String sheetName = "";
	
	private int sheetIdx = 1;
	
	/**
	 * Excel写入类
	 */
	private ExcelWriter excelWriter;
	
	/**
	 * Excel数据类
	 */
	private ExcelWordData data;
	
	private ExcelCartoonStyle style;
	
	private ExcelEnum enums;
	
	/**
	 * 列索引
	 */
	private int colIdx = 0;
	private int rowIdx = 1;
	

	/**
	 * 设置标签名
	 * @param sheetName
	 */
	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	/**
	 * 设置索引
	 * @param sheetIdx
	 */
	public void setSheetIdx(int sheetIdx) {
		this.sheetIdx = sheetIdx;
	}
	
	/**
	 * 设置数据
	 * @param data
	 */
	public void setData(ExcelWordData data) {
		this.data = data;
	}

	/**
	 * 写Sheet
	 * @param excelWriter
	 */
	public void writeSheet(ExcelWriter excelWriter){
		
		this.excelWriter = excelWriter;
		
		//初始样式构造
		style = new ExcelCartoonStyle();
		style.loadContentStyle(excelWriter);
		style.loadTitleStyle(excelWriter);
		style.loadContentLeftStyle(excelWriter);
		
		this.enums = new ExcelEnum();
		this.enums.loadEnum();
		
		//写Sheet
		this.createSheet();
		
		//添加Title
		this.colIdx = 0;
		this.writeTitle();
		
		//首行冻结
		this.excelWriter.getActiveSheet().createFreezePane(0, 1, 0, 1);
		
		//写入数据
		if(this.data.getCartoonList(sheetName) != null && this.data.getCartoonList(sheetName).size() > 0 ){
			
			this.writeData();
			
		}
		
		System.out.println(this.sheetName+"标签创建完毕..........");
		
	}
	
	/**
	 * 新建标签
	 */
	private void createSheet(){	
		this.excelWriter.locateSheet(this.sheetIdx);
		this.excelWriter.renameSheet(this.sheetName);	
	}
	
	/**
	 * 写入标题
	 */
	private void writeTitle(){
		this.createTitle("动漫名称",200);
		this.createTitle("动漫其他名称",200);
		this.createTitle("动漫标签",200);
		this.createTitle("动漫详细名称",200);	
		this.createTitle("动漫清晰度",75);
		this.createTitle("动漫地区",75);	
		this.createTitle("动漫来源",75);
		this.createTitle("动漫种类",75);
		this.createTitle("动漫状态",75);
		this.createTitle("动漫集数",75);
		this.createTitle("动漫播放时间",100);
		this.createTitle("动漫体积",75);
		this.createTitle("动漫存储位置",450);
	}
	
	private void writeData(){
		
		List<String> cartoonList = this.data.getCartoonList(sheetName);
		
		for(String cartoonId:cartoonList){
			
			List<Record> cartoonDetail = this.data.getCartoonDetail(cartoonId);
			
			//动漫数量
			int size = this.data.getCartoonSize(cartoonId);
			
			int idx = 1;
			
			for(Record record:cartoonDetail){
				
				this.colIdx = 0;
				this.excelWriter.locateRow(this.rowIdx);
				
				if(idx == 1){
					
					this.excelWriter.setRowSpan(0, cartoonDetail.size());
					this.excelWriter.setRowSpan(1, cartoonDetail.size());
					this.excelWriter.setRowSpan(2, cartoonDetail.size());
					
					this.createData(record.getString("CAN_NAME"), 0,size);
					
					//其他名称换行处理
					String otherName = record.getString("CAN_NAME_OTHER");
					if(otherName==null){
						otherName="";
					}
					String[] others = Basic.split(otherName, ",","，");	
					int length = 0;
					String result = "";
					if(others != null){
						length = others.length * 25 - cartoonDetail.size() * 25;
						
						for(String other:others){
							result +=  "\n"+ other;
						}
						
					}
					
					if(!"".equals(result)){
						result = result.substring(1);
					}
					if(length <= 0){
						this.createData(result, 25,size);
					}else{
						this.createData(result, length,size);
					}
								
					String tag = record.getString("RES_TAG_MEMO");
					if(tag != null){
						this.createData(tag.replaceAll("，", ","), 0,size);
					}else{
						this.createData("", 0,size);
					}
					
				} else {
					this.createData("", 0,size);
					this.createData("", 25,size);
					this.createData("", 0,size);
				}
					
				this.createData(record.getString("CAN_DETAIL_NAME"), 0,size);
				this.createData(this.enums.getQua(record.getString("CAN_QUA")), 0,size);
				this.createData(this.enums.getArea(record.getString("CAN_AREA")), 0,size);
				this.createData(this.enums.getSrc(record.getString("CAN_SOURCE")), 0,size);
				this.createData(this.enums.getKind(record.getString("CAN_KIND")), 0,size);
				this.createData(this.enums.getStat(record.getString("CAN_STAT")), 0,size);
				this.createData(record.getString("CAN_TOTAL"), 0,size);
				this.createData(record.getString("CAN_TIME").substring(0,10), 0,size);
				this.createData(record.getString("RES_STORE_SIZE"), 0,size);
				this.createData(record.getString("DISK_ID")+":"+record.getString("RES_STORE_PLACE"), 0,size);
				
				this.rowIdx ++ ;
				
				idx++;
				
			}
			
		}
		
	}
	
	private void createTitle(String titleName,int width){
		
		this.excelWriter.locateRow(0);
	
		this.excelWriter.setCellValue(this.colIdx,titleName);
		this.excelWriter.setRowHeight(30);
		this.excelWriter.setColWith(this.colIdx, width);
			
		this.excelWriter.setStyle(style.getTitleStyle());
		
		this.colIdx ++ ;
		
	}
	
	private void createData(String dataText,int rowHeight,int size){
		
		this.excelWriter.setCellValueRich(this.colIdx,dataText);
		
		if(rowHeight > 0){
			this.excelWriter.setRowHeight(rowHeight);
		}
		
		if(size >= 0 && size < 5 ){
			this.excelWriter.setStyle(style.getGrayStyle());
		}
		
		else if(size >= 5 && size < 16 ){
			this.excelWriter.setStyle(style.getGreenStyle());
		}
		
		else if(size >= 16 && size < 35 ){
			this.excelWriter.setStyle(style.getBlueStyle());
		}
		
		else if(size >= 35 && size < 75 ){
			this.excelWriter.setStyle(style.getYellowStyle());
		}
		
		else if(size >= 75 && size < 135 ){
			this.excelWriter.setStyle(style.getOrangeStyle());
		}
		
		else if(size >= 135 && size < 300 ){
			this.excelWriter.setStyle(style.getPinkStyle());
		}
		
		else if(size >= 300 ){
			this.excelWriter.setStyle(style.getRedStyle());
		}
		
		else{
			this.excelWriter.setStyle(style.getContentStyle());
		}
		
		
		this.colIdx ++ ;
		
		
	}
	
	private void createLeftData(String dataText,int rowHeight,int size){
		
		this.excelWriter.setCellValueRich(this.colIdx,dataText);
		
		if(rowHeight > 0){
			this.excelWriter.setRowHeight(rowHeight);
		}
		
		if(size >= 0 && size < 5 ){
			this.excelWriter.setStyle(style.getGrayLeftStyle());
		}
		
		else if(size >= 5 && size < 16 ){
			this.excelWriter.setStyle(style.getGreenLeftStyle());
		}
		
		else if(size >= 16 && size < 35 ){
			this.excelWriter.setStyle(style.getBlueLeftStyle());
		}
		
		else if(size >= 35 && size < 75 ){
			this.excelWriter.setStyle(style.getYellowLeftStyle());
		}
		
		else if(size >= 75 && size < 135 ){
			this.excelWriter.setStyle(style.getOrangeLeftStyle());
		}
		
		else if(size >= 135 && size < 300 ){
			this.excelWriter.setStyle(style.getPinkLeftStyle());
		}
		
		else if(size >= 300 ){
			this.excelWriter.setStyle(style.getRedLeftStyle());
		}
		
		else{
			this.excelWriter.setStyle(style.getContentLeftStyle());
		}
		
		
		this.colIdx ++ ;
		
		
	}
	
}
